Gson

您所在的位置:网站首页 gson jsonfield Gson

Gson

2023-03-11 18:50| 来源: 网络整理| 查看: 265

Learn to use Google GSON library to deserialize or parse a JSON array to a Java array or List object. It’s worth mentioning that JSON has only array datatype. Java has both – arrays and lists.

Table Of Contents 1. Parsing JSON with Array as Root Object1.1. Converting JSON Array to Array of Objects1.2. Converting JSON Array to List of Objects2. Parsing JSON with Array as Member2.1. Member JSON Array to Java Array2.2. Member JSON Array to Java List 1. Parsing JSON with Array as Root Object

To parse JSON, with array as root, we can use the following method syntax. Here ArrayItem is the class type of data elements in the array.

ArrayItem[] userArray = new Gson().fromJson(jsonSource, ArrayItem[].class);

For converting such JSON array to a List, we need to use TypeToken class.

Type listType = new TypeToken(){}.getType(); ArrayList list = gson.fromJson(jsonSource, listType);

For demo purposes, we are using User.java as the JSON element type.

public class User { private long id; private String name; // Getters and Setters } 1.1. Converting JSON Array to Array of Objects

Java program to deserialize JSON array as root – to Java array of objects.

String userJson = "[{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]"; Gson gson = new Gson(); User[] userArray = gson.fromJson(userJson, User[].class); for(User user : userArray) { System.out.println(user); }

Program output.

User [id=1, name=Alex] User [id=2, name=Brian] User [id=3, name=Charles] 1.2. Converting JSON Array to List of Objects

Java program to deserialize JSON array as root – to Java list of objects.

String userJson = "[{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]"; Gson gson = new Gson(); Type userListType = new TypeToken(){}.getType(); ArrayList userArray = gson.fromJson(userJson, userListType); for(User user : userArray) { System.out.println(user); }

Program output.

User [id=1, name=Alex] User [id=2, name=Brian] User [id=3, name=Charles] 2. Parsing JSON with Array as Member

Gson parses JSON arrays as members without difficulty if they are non-root objects. We can use the fromJson() method in the usual manner and it will parse the JSON array correctly to the required java array or list.

2.1. Member JSON Array to Java Array

Java program to deserialize JSON array as member object – to Java array of objects as member field.

public class Department { private long id; private String name; private User[] users; //Getters and Setters } String departmentJson = "{'id' : 1, " + "'name': 'HR'," + "'users' : [" + "{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]}"; Gson gson = new Gson(); Department department = gson.fromJson(departmentJson, Department.class); System.out.println(department);

Program output.

Department [id=1, name=HR, users=[User [id=1, name=Alex], User [id=2, name=Brian], User [id=3, name=Charles]]] 2.2. Member JSON Array to Java List

Java program to deserialize JSON array as member object – to Java list of objects a member field.

public class Department { private long id; private String name; private List users; //Getters and Setters } String departmentJson = "{'id' : 1, " + "'name': 'HR'," + "'users' : [" + "{'name': 'Alex','id': 1}, " + "{'name': 'Brian','id':2}, " + "{'name': 'Charles','id': 3}]}"; Gson gson = new Gson(); Department department = gson.fromJson(departmentJson, Department.class); System.out.println(department);

Program output.

Department [id=1, name=HR, users=[User [id=1, name=Alex], User [id=2, name=Brian], User [id=3, name=Charles]]]

Drop me your question related to gson parse JSON array to java lists and arrays.

Happy Learning !!

Sourcecode on Github

Related posts: Gson – Read and Write JSON GSON – Serialize and deserialize JSON to Set Gson – Pretty Print JSON Parsing and Extracting HTML with Jsoup Gson – Quick Guide Gson – JsonParser


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3